// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Windows.Forms;
using svs.Mobile.BeyondMedia.FeedCore;
using svs.Mobile.BeyondMedia.FeedCore.Helpers;
using svs.Mobile.BeyondMedia.Forms;
using svs.Mobile.BeyondMedia.Objects;
using svs.Mobile.BeyondMedia.UiCore;

namespace svs.Mobile.BeyondMedia
{
	internal static class Program
	{
		//static OneInstance m_instanceChecker = new OneInstance();
		private static CmdArgs _cmdArgs = new CmdArgs();
		private static SplashForm _Splash;

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[MTAThread]
		private static void Main(string[] args)
		{
			// Add Global Exception Handler
			AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

			//Make sure device does not go to sleep during startup
			CoreHelper.KeepDeviceAwake();

			ShowSplashScreen();

			//Find and kill all other instancves of BeyondPod
			ProcessHelper.KilExistingInstances();
			Helpers.DoPostInstalProcessing();

			//Make sure we have enough mempry to run
			CoreHelper.FreeProgramMemoryIfNeeded(300*1024);

			_cmdArgs = new CmdArgs(args);
			Application.Run(new Master(_cmdArgs));

			//Sometimes Windows Media Player causes the entire process to hang upon exit even with all windows closed
			//and no message pump. This is the only way that we found around it.
			ProcessHelper.MyProcess.Kill();
		}

		#region Splash Screen Management

		public static void ShowSplashScreen()
		{
			_Splash = new SplashForm();
			_Splash.Show();
		}

		public static void CloseSplashScreen()
		{
			_Splash.Close();
		}

		public static void WriteSplashInfo(string info)
		{
			_Splash.UpdateInfoLabel(info);
			Application.DoEvents();
		}

		#endregion


		private static void OnUnhandledException(Object sender, UnhandledExceptionEventArgs e)
		{
			Exception ex = e.ExceptionObject as Exception;
			if (ex != null)
			{
				CoreHelper.WriteLogEntry("Fatal Exception Handler: " + ex.Message + "\n" + ex.StackTrace + "\n\n");

				if (ex is OutOfMemoryException)
				{
					Helpers.NotifyOutOfMemory(ex);
				}

				FeedRepository.SaveRepository();
				CoreHelper.SaveTraceBuffer();
				MessageBoxEx.Show(
					"BeyondPod encountered an error and needs to close!\nError: " + ex.Message +
					"\n\nAdditional information is available in BeyondPodLog.txt file",
					"Fatal Error!");
			}
		}
	}
}